using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Universal.Api.Attributes;
using Universal.Application.Contracts.Common;
using Universal.Application.Contracts.Dto.Role;
using Universal.Application.Repositories;

namespace Universal.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class RoleController : ControllerBase
{
    private readonly IRoleService _roleService;
    public RoleController(IRoleService roleService)
    {
        _roleService = roleService;
    }


    /// <summary>
    /// 创建角色
    /// </summary>
    [HttpPost]
    [RequirePermission("role.Create")]
    public async Task<IActionResult> CreateRoleAsync([FromBody] CreateRoleDto dto)
    {
        if (!ModelState.IsValid)
        {
            return Ok(ApiResponse<string>.Failure(400, "参数校验失败"));
        }

        var result = await _roleService.CreateRoleAsync(dto);
        return Ok(result);

    }

    /// <summary>
    /// 获取单个角色
    /// </summary>
    [HttpGet("{id}")]
    public async Task<IActionResult> GetByIdRoleAsync(Guid id)
    {
        var result = await _roleService.GetRoleByIdAsync(id);
        return Ok(result);

    }

    /// <summary>
    /// 更新角色
    /// </summary>
    [HttpPut("{id}")]
    [RequirePermission("role.Edit")]
    public async Task<IActionResult> UpdateRoleAsync(Guid id, [FromBody] UpdateRoleDto dto)
    {
        if (!ModelState.IsValid)
        {
            return Ok(ApiResponse<string>.Failure(400, "参数校验失败"));
        }

        var result = await _roleService.UpdateRoleAsync(id, dto);
        return Ok(result);
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    [HttpDelete("{id}")]
    [RequirePermission("role.Delete")]
    public async Task<IActionResult> DeleteRoleAsync(Guid id)
    {
        var result = await _roleService.DeleteRoleAsync(id);
        return Ok(result);
    }

    /// <summary>
    /// 分配权限
    /// </summary>
    [HttpPut("assignpermissions/{id}")]
    [RequirePermission("role.Edit")]
    public async Task<IActionResult> AssignPermissionsAsync(Guid id, [FromBody] AssignPermissionsDto dto)
    {
        if (!ModelState.IsValid)
        {
            return Ok(ApiResponse<string>.Failure(5000, "参数校验失败"));
        }

        var result = await _roleService.AssignPermissionsAsync(id, dto);
        return Ok(result);
    }

    /// <summary>
    /// 分配菜单
    /// </summary>
    [HttpPut("assignmenus/{id}")]
    [RequirePermission("role.Edit")]
    public async Task<IActionResult> AssignMenusAsync(Guid id, [FromBody] AssignMenusDto dto)
    {
        var result = await _roleService.AssignMenusAsync(id, dto);
        return Ok(result);
    }



    /// <summary>
    /// 获取分页角色列表
    /// </summary>
    [HttpGet("paged")]
    [RequirePermission("role.View")]
    public async Task<IActionResult> GetRolesPagedAsync([FromQuery] int pageIndex, [FromQuery] int pageSize)
    {
        var result = await _roleService.GetRolesPagedAsync(pageIndex, pageSize);
        return Ok(result);
    }

    [HttpPut("enableRole/{id}")]
    [RequirePermission("role.Edit")]
    public async Task<IActionResult> EnableRoleAsync(Guid id)
    {
        var result = await _roleService.EnableRoleAsync(id);
        return Ok(result);
    }

    [HttpPut("disableRole/{id}")]
    [RequirePermission("role.Edit")]
    public async Task<IActionResult> DisableRoleAsync(Guid id)
    {
        var result = await _roleService.DisableRoleAsync(id);
        return Ok(result);
    }


    // 用于角色界面中分配菜单时需要的数据
    [HttpGet("menus")]
    public async Task<IActionResult> GetRoleViewMenus()
    {
        var result = await _roleService.GetRoleViewMenuAsync();
        return Ok(result);
    }

    [HttpGet("permissions")]
    public async Task<IActionResult> GetRoleViewPermissions()
    {
        var result = await _roleService.GetRoleViewpermissionAsync();
        return Ok(result);
    }

}
